<?php

class Admin_MuralrecadosController extends App_Controller_Action
{

    private $_table     = 'MURAL_RECADOS';
    private $_pk        = 'MRC_ID';
    private $_order_col = 'MRC_DAT';
    private $_order     = 'DESC';
    private $_sta_col   = 'MRC_STA';
    private $obj        = '';
    private $form       = '';
    
    private $_PER_ID;
    private $_USU_ID;
    
    public function init()
    {
        if($this->getRequest()->getActionName() == 'edit') $this->form = new Admin_Form_Muralrecados();
        
        $auth = Zend_Auth::getInstance();
        $s_user = $auth->getStorage();
        $this->_PER_ID = $s_user->read()->PER_ID;
        $this->_USU_ID = $s_user->read()->USU_ID;
    }

    public function indexAction() {
        //$this->_helper->redirector('edit');
        
        Zend_Loader::loadClass('tools','library');
        Zend_Loader::loadClass('datetools','library');
        Zend_Loader::loadClass('elemform','library');
        Zend_Loader::loadClass('dominio','library');
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/validation/dist/jquery.validate.min.js');
        
        $this->view->headLink()->prependStylesheet($this->_urlbase.'/public/media/js/chosen/chosen.min.css'); 
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/chosen/chosen.jquery.min.js');
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/view/s_muralrecados.js');
        
        $where = 'm.'.$this->_pk . ' > 0';
        
        
        //$filtro = $this->_getParam('itfiltro', '1'); 
        //$cbfiltro = $this->_getParam('cbfiltro', ''); 
        
        //if($filtro != '') $where .= ' and m.MRC_STA = ' . $filtro;
        //if($cbfiltro > '') $where .= ' and m.ALU_ID = ' . $cbfiltro;
        
        $this->view->filtro = $filtro;
        $this->view->cbfiltro = $cbfiltro;
        
        if($this->_PER_ID <> 1) $where .= ' AND USU_ID = '.$this->_USU_ID;
        
        $query = 'SELECT SQL_CACHE m.* FROM '.$this->_table.' m WHERE '.$where.' ORDER BY m.'.$this->_order_col.' '.$this->_order.', m.MRC_HOR DESC';
        $result = $this->db->fetchAll($query);
        $this->view->result = $this->paginacao($result);
        
    }
    
    public function editAction() {
        
        Zend_Loader::loadClass('tools','library');
        Zend_Loader::loadClass('datetools','library');
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/validation/dist/jquery.validate.min.js');
        
        $this->view->headLink()->prependStylesheet($this->_urlbase.'/public/media/js/chosen/chosen.min.css'); 
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/chosen/chosen.jquery.min.js');
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/view/s_muralrecados.js');
        
        $this->view->form = $this->form;
        if($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            try {
                if($this->form->isValid($formData)) {
                    $id = (int) $this->form->getValue($this->_pk);
                    
                    $data = $formData;//$this->form->getValues(); 
                    
                    unset($data['Salvar']);
                    unset($data['destino']);
                    
                    echo 'Enviando mensagens...';
                    
                    if($id == 0){ 
                        
                        $ALU_ID = isset($data['ALU_ID']) ? implode(',', $data['ALU_ID']):'';
                        $CBS_ID = isset($data['CBS_ID']) ? implode(',', $data['CBS_ID']):'';
                        $CUR_ID = isset($data['CUR_ID']) ? implode(',', $data['CUR_ID']):'';
                        
                        $arr = array(
                            'MRC_DAT' => date('Y-m-d'),
                            'MRC_HOR' => date('H:i:00'),
                            'MRC_ASS' => $data['MRC_ASS'],
                            'MRC_TXT' => $data['MRC_TXT'],
                            'USU_ID'  => Zend_Auth::getInstance()->getStorage()->read()->USU_ID,
                            'ALU_ID'  => $ALU_ID,
                            'CBS_ID'  => $CBS_ID,
                            'CUR_ID'  => $CUR_ID,
                            'MRC_STA' => '1',
                        );
                        $this->db->insert($this->_table,$arr);
                        $id = $this->db->lastInsertId(); 
                        
                        //alunos
                        if($formData['destino'] == 'a'){
                            foreach($data['ALU_ID'] as $aluno){
                                
                                $query = 'SELECT SQL_CACHE USU_ID FROM ALUNO WHERE ALU_ID = '.$aluno;
                                $row = $this->db->fetchRow($query);
                                
                                $arr2 = array(
                                    'MRC_ID'  => $id,
                                    'USU_ID'  => $row['USU_ID'],
                                    'ALU_ID'  => $aluno,
                                    'MRM_DAT' => $arr['MRC_DAT'],
                                    'MRM_HOR' => $arr['MRC_HOR'],
                                    'MRM_ASS' => $arr['MRC_ASS'],
                                    'MRM_TXT' => $arr['MRC_TXT'],
                                    'MRM_STA' => '1',
                                );
                                
                                $this->db->insert('MURAL_RECADOS_MSG',$arr2);
                            }
                        // cursos
                        } else if($formData['destino'] == 'c'){
                            foreach($data['CBS_ID'] as $curso){    
                                
                                $query = 'SELECT SQL_CACHE DISTINCT m.ALU_ID '
                                        . 'FROM MATRICULA m, CURSO c, CURSOBASE cb '
                                        . 'WHERE m.CUR_ID = c.CUR_ID AND cb.CBS_ID = c.CBS_ID '
                                        . 'AND c.CUR_STA = 1 AND cb.CBS_ID = '.$curso.'';
                                $result = $this->db->fetchAll($query);
                                
                                foreach($result as $alu){
                                    
                                    $query = 'SELECT SQL_CACHE USU_ID FROM ALUNO WHERE ALU_ID = '.$alu['ALU_ID'];
                                    $row = $this->db->fetchRow($query);
                                    
                                    $arr2 = array(
                                        'MRC_ID'  => $id,
                                        'USU_ID'  => $row['USU_ID'],
                                        'ALU_ID'  => $alu['ALU_ID'],
                                        'MRM_DAT' => $arr['MRC_DAT'],
                                        'MRM_HOR' => $arr['MRC_HOR'],
                                        'MRM_ASS' => $arr['MRC_ASS'],
                                        'MRM_TXT' => $arr['MRC_TXT'],
                                        'MRM_STA' => '1',
                                    );

                                    $this->db->insert('MURAL_RECADOS_MSG',$arr2);
                                }
                            }
                        //grupos
                        } else if($formData['destino'] == 'g'){
                            foreach($data['CUR_ID'] as $grupo){    
                                
                                echo $query = 'SELECT SQL_CACHE DISTINCT m.ALU_ID '
                                        . 'FROM MATRICULA m, CURSO c '
                                        . 'WHERE m.CUR_ID = c.CUR_ID '
                                        . 'AND c.CUR_STA = 1 AND c.CUR_ID = '.$grupo.'';
                                $result = $this->db->fetchAll($query);
                                
                                foreach($result as $alu){
                                    
                                    $query = 'SELECT SQL_CACHE USU_ID FROM ALUNO WHERE ALU_ID = '.$alu['ALU_ID'];
                                    $row = $this->db->fetchRow($query);
                                    
                                    $arr2 = array(
                                        'MRC_ID'  => $id,
                                        'USU_ID'  => $row['USU_ID'],
                                        'ALU_ID'  => $alu['ALU_ID'],
                                        'MRM_DAT' => $arr['MRC_DAT'],
                                        'MRM_HOR' => $arr['MRC_HOR'],
                                        'MRM_ASS' => $arr['MRC_ASS'],
                                        'MRM_TXT' => $arr['MRC_TXT'],
                                        'MRM_STA' => '1',
                                    );

                                    $this->db->insert('MURAL_RECADOS_MSG',$arr2);
                                }
                                
                            }
                        }
                        
                    }
                    else{
                        $this->db->update($this->_table,$data, $this->_pk.' = ' . $id);
                    }
                    
                    $this->_helper->redirector('index');
                } else {
                    $this->form->populate($formData);
                }
            } catch(Exception $ex) {
                Zend_Loader::loadClass('errordb','library');
                echo '<script>alert("'.Errordb::descricaoError($ex->getCode()).'");</script>';
                echo $ex->getMessage();
                $this->form->populate($formData);
            }
        } else {
            $id = $this->_getParam('id', 0);
            if ($id > 0) {
                $query = 'SELECT SQL_CACHE * FROM MURAL_RECADOS WHERE '.$this->_pk . ' = ' . $id;
                $data = $this->db->fetchRow($query);
                $this->form->populate($data);
                $this->view->data = $data;
            }
        }
    }

    public function deleteAction() {
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender();  
        $ok = FALSE;
        $id = $this->_getParam('id', 0);
        if ((int)$id > 0 ) {   
            //$data = array($this->_sta_col => 0);
            //$ok = (bool) $this->obj->update($data, $this->_pk . ' = ' . $id);
            $this->db->delete($this->_table,$this->_pk . ' = ' . $id);
            
        }       
        echo json_encode(array('ok' => $ok));
    }

}